﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>fixed - test</title>
</head>

<body>
    <div id="iDiv" style="position:absolute;top:50px;left:200px;border:3px solid #f00;width:300px;height:200px;">
	    <div id="title" style="height:28px;background:#00f;margin:2px;"></div>
	</div>
    <div id="iDiv1" style="position:absolute;top:50px;left:700px;border:3px solid #f00;width:300px;height:200px;">
	    <div id="title1" style="height:28px;background:#00f;margin:2px;"></div>
	</div>
	<script type="text/javascript">
// 因为需要操作body元素，所以要放到body元素的下面
var ie6 = window.ActiveXObject && !window.XMLHttpRequest,
    html = document.getElementsByTagName('html')[0],
	dom = document.compatMode == 'CSS1Compat' ? document.documentElement : document.body,

compat = function( doc )
{
    doc = doc || document;
	return doc.compatMode === 'CSS1Compat' ? doc.documentElement : doc.body;
},

getScroll = function()
{
    return {
	    left: dom.scrollLeft,
		top: dom.scrollTop
	}
};

var setAbsolute = function( el )
{
    var style = el.style;
	
	if( ie6 )
	{
	    style.removeExpression('left');
		style.removeExpression('top');
	}
	else
	    style.position = 'absolute';
};

var setFixed = (function(){
    if( ie6 )
	{
	    if( html.currentStyle.backgroundAttachment !== 'fixed' && document.body.currentStyle.backgroundAttachment !== 'fixed' )
		{
		    html.style.zoom = 1;
			html.style.backgroundImage = 'url(about:blank)';
			html.style.backgroundAttachment = 'fixed';
		}
	}
	
	return function( el )
	{
	    var style = el.style;
		
		if( ie6 )
		{
		    var sLeft = getScroll().left,
			    sTop = getScroll().top,
				left = parseInt(style.left) - sLeft,
				top = parseInt(style.top) - sTop,
				txt = document.compatMode == 'CSS1Compat' ? 'this.ownerDocument.documentElement' : 'this.ownerDocument.body';
			
			setAbsolute( el );
			
			style.setExpression( 'left', txt + '.scrollLeft +' + left );
			style.setExpression( 'top', txt + '.scrollTop +' + top );
		}
		else
		    style.position = 'fixed';
	};
})();

var elem = document.getElementById('iDiv1');
setFixed( elem );
	</script>
    <div style="height:1400px;"></div>	
</body>
</html>